package com.chain.qs.modules.biz.web;

import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.common.web.BaseController;
import com.chain.qs.modules.biz.entity.Handover;
import com.chain.qs.modules.biz.service.HandoverService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 值班管理Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/handover")
public class HandoverController extends BaseController {

    @Resource
    private HandoverService handoverService;

    @ModelAttribute
    public Handover get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return handoverService.get(id);
        } else {
            return new Handover();
        }
    }

    //@RequiresPermissions("sys:handover:view")
    @RequestMapping(value = {"detail", ""})
    public String detail(Model model) {
        model.addAttribute("handover",handoverService.getHandover());
        return "modules/biz/handoverDetail";
    }

    //@RequiresPermissions("sys:handover:view")
    @RequestMapping(value = "form")
    public String from(Handover handover, Model model) {
        model.addAttribute(handover);
        return "modules/biz/handoverForm";
    }

//    @RequiresPermissions("sys:handover:edit")
    @RequestMapping(value = "save")
    public String save(HttpServletResponse response,Handover handover) {
        return renderString(response, handoverService.saveAndDel(handover));
    }

    //执行开关班次操作
    @RequestMapping(value = "switch")
    public String switchTo(HttpServletResponse response,Handover handover,boolean state) {
        handover.setIsActive(state?1:0);
        return renderString(response, handoverService.switchTo(handover));
    }
    //查看开关状态
    @ResponseBody
    @RequestMapping(value = "getHandoverState")
    public Object getHandoverState(HttpServletResponse response) {
        int index = handoverService.getHandoverState();
        if(index == 1){
            return renderString(response, "ON");
        }else {
            return renderString(response, "OFF");
        }
    }

}
